package angry.courses.servlets;

import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

import javax.servlet.ServletException;

import angry.courses.entities.StudentEntity;

public class DeleteServlet extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		final String regNr = req.getParameter("regnr");
	
		StudentDelegate studentDelegate = new StudentDelegate();
		final StudentEntity student = studentDelegate.getStudent(regNr);
		
		RegisterDelegate registerDelegate = new RegisterDelegate();
		registerDelegate.deleteCoursesForStudent(student);
		
		studentDelegate.deleteStudent(student);

		res.setContentType("text/html");
		PrintWriter out = res.getWriter();
		out.println(new HtmlFormatter() {
			public String title() {
				return "Registration Deleted";
			}

			public void body(StringBuffer buf) {
				buf.append(heading(3, "Completed"));
				buf.append("Registration of <b>" + student.getName()
					+ "</b> deleted.<br>");
				buf.append("Click " + link("here", "index.html") +
					   " to go back.");
			}
		}.htmlTag());
		out.close();
    }
}
